// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
import world from './world';
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    
    @property({
        displayName:"动画Node",
        type:cc.Node,
    })
    mcNode:cc.Node = null;

    @property({
        displayName:"地图Node",
        type:cc.Node,
    })
    mapNode:cc.Node = null;

    @property({
        displayName:"速度",
        tooltip:'移动的速度，值越大跑的越快'
    })
    speed:number= 3;

    @property({
        displayName:"弹跳力",  
        tooltip:'跳起的力量，如果重量不变，值越大跑的越高'
    })
    bounce:number= 15;

   
 
   
    @property({type: cc.AudioClip })
    jumpSou:cc.AudioClip = null;  
   
   
    // LIFE-CYCLE CALLBACKS:
   
     private keyArr:Array<string>=[];
     private anim:cc.Animation;
     private map:any;
     onLoad () {
        cc.director.getCollisionManager().enabled = true;
        this.keyArr=[];
        cc['eventManager'].addListener({event:cc['EventListener'].KEYBOARD,
			onKeyPressed:this.onKeyPressed.bind(this),
            onKeyReleased:this.onKeyReleased.bind(this)
        },this.node);
        this.anim = this.mcNode.getComponent(cc.Animation);
       
        let _this=this;
        this.map=this.mapNode.getComponent("Map");
        this.map.isHit={"问号":true,"p0":true,"p1":true,"p5":true,"p6":true,"p11":true,"p15":true};
        this.map.Init=function(){
            _this.node.parent=_this.map.getLayerNodeFun("人物层");
            _this.node.y-=640;
        }
     
     }
     private run(){
        if(window['xueInt']<=0){return;}
        if(this.mcID=='待机'||this.mcID=='伤'){
            this.mcID='跑';
            this.anim.play('run');
            
        }
    }
    private onCollisionEnter(other, self){
         if(other.tag==1){
             if(this.world.pzbo){

             }else{
                 this.world.bounce=10;
             }
         }
    }
    private jump(i=null){
        if(window['xueInt']<=0){return;}
        if(i!=null&&this.world.pzbo){
            this.world.bounce=i;
            cc.audioEngine.play(this. jumpSou, false, 0.3);
           
        }
       
           
        
    }
    private mcID="";
    private stand(){
        if(window['xueInt']<=0){return;}
        if(this.mcID!='待机'&&this.world.pzbo){
            this.mcID='待机';
            this.anim.play('stand');
        }
    }
    private onKeyPressed(keyCode, event){
        if(window['xueInt']<=0){return;}//
        switch(keyCode) {
            case cc.macro.KEY.a:
            case cc.macro.KEY.left:
                 let sy=this.keyArr.indexOf("左");
                 if(sy==-1){
                    this.keyArr.unshift("左")
                 }
                this.run();
                break;
            case cc.macro.KEY.d:
            case cc.macro.KEY.right:
                let sy2=this.keyArr.indexOf("右");
                if(sy2==-1){
                    this.keyArr.unshift("右")
                }
                this.run();
                break;
            case cc.macro.KEY.k:
                //this.keyTiao();
                this.tiaoBo=true;
                break;
        }
    }
    private tiaoBo:boolean=false;
    private onKeyReleased(keyCode, event){
        switch(keyCode) {
            case cc.macro.KEY.a:
            case cc.macro.KEY.left:
              let sy=this.keyArr.indexOf("左");
              if(sy!=-1){
                  this.keyArr.splice(sy,1);
              }
              break;
            case cc.macro.KEY.d:
            case cc.macro.KEY.right:
                let sy2=this.keyArr.indexOf("右");
                if(sy2!=-1){
                   this.keyArr.splice(sy2,1);
                }
                break;
            case cc.macro.KEY.k:
                //this.keyTiao();
                this.tiaoBo=false;
                break;
        }
    }
    private world:world
    start () {
         this.world=this.node.getComponent("world");
         let _this=this;
         this.world.hitHeadFun=function(id){
             let n:cc.Node=_this.map.showNodeObj[id];
             if (n != null) {
                 console.log(n["info"],id)
                 if(n.name=="问号"&&n['currentFrame']<n['totalFrame']){
                    cc['tween'](n)
                    .by(0.1, {y: 5})      // node.scale === 4 (2+2)
                    .by(0.1, {y: -5})      // node.scale === 5
                    .start()
                   
                    if(n['info']==null||n['info'].type==null||n['info'].type=="出怪"){
                        let pn=window["pool"]["getPrefab"]("ygou");
                        if(pn!=null){
                            pn.x=n.x+16;
                            pn.y=n.y+2;
                            pn.parent=n.parent;
                            pn.zIndex=n.zIndex-0.5;
                            pn.getComponent("world").bounce=10;
                          //  pn["bomb"].Init();
                        }
                        n['gotoAndStop'](n['totalFrame']);
                    }else if(n['info'].type=="爆炸"){
                        _this.addMonster(n)
                    }
                    
                  
                 }else if(n.name=="p1"){
                    if(n.name=="p1"){
                        _this.addMonster(n)
                    }
                 }
             }
             
         }
        
         this.world.updateMapLocation=function (){
            let rect=_this.map.MapRect;
            if(rect==null){
                return;//
            }
            let nx=200;
            let cp=new cc.Vec2(_this.node.x*-1+nx,0);
            //区域限制
            if(rect.width>cc.view.getVisibleSize().width){//
                if(_this.node.x<rect.x+nx){
                    cp.x=rect.x*-1;
                }
                if(_this.node.x>rect.x+rect.width-cc.view.getVisibleSize().width+nx){
                    cp.x=(rect.x+rect.width-cc.view.getVisibleSize().width)*-1;
                }
            }else{
                cp.x=rect.x*-1+nx-rect.width/2;
            }
            if(cp.x!=_this.mapNode.x&&_this.map!=null){
                _this.map.setLocationFun(cp.x);
            }
            
        }
    }
    addMonster(n){
        let pn=window["pool"]["getPrefab"]("polie");
        if(pn!=null){
            pn.x=n.x;
            pn.y=n.y;
            pn.parent=n.parent;
            pn.zIndex=n.zIndex+0.5;
            pn["bomb"].Init();
        }
        this.map.removeMapNode(n);
        n.destroy();
    }
    update (dt) {
        if(this.tiaoBo){
            this.jump(this.bounce);
        }
        this.world.pzbo=this.world.isJump();
        if(!this.world.pzbo&&this.mcID!='跳'){
            this.mcID='跳';
            this.anim.play('jump');
        }else if(this.world.pzbo&&this.mcID=='跳'){
            if(this.keyArr[0]==="左"||this.keyArr[0]==="右"){
                this.mcID='跑';
                this.anim.play('run');
            }else {
                this.mcID='待机';
                this.anim.play('stand');
            }
        }
        if(this.keyArr.length==0){
            this.stand();

        }else if(this.keyArr[0]==="左")
		{
            this.mcNode.scaleX=1;
            this.world.setXY(this.node.x+this.speed*this.mcNode.scaleX*-1);
       
		}else if(this.keyArr[0]==="右")
		{
            this.mcNode.scaleX=-1;
            this.world.setXY(this.node.x+this.speed*this.mcNode.scaleX*-1);
      
      
        }
    }
}
